package com.hbdl.web.sys.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.hbdl.common.base.ServiceMybatis;
import com.hbdl.common.mybatis.dao.MybatisDaoUtil;
import com.hbdl.common.utils.PMSUtils;
import com.hbdl.common.utils.PasswordEncoder;
import com.hbdl.web.sys.model.User;

/**
 * 用户管理服务
 * 
 * @author ql
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Service
public class SysUserService extends ServiceMybatis<User> {

	
	private MybatisDaoUtil<Map> dao = new MybatisDaoUtil<Map>(Map.class);

	public PageInfo<Map> findPageInfo(Map params) {
		PageInfo page = dao.selectPage("userMapper.queryUser", params);

		return page;
	}
	
	
	public Map queryUser(Map params) {
		Map user = dao.selectOne("userMapper.queryUser", params);
		
		return user;
	}
	
	public List queryListUser(Map params) {
		return dao.selectList("userMapper.queryUser", params);
	}
	public List queryUserForTransfer(Map params) {
		return dao.selectList("userMapper.queryUserForTransfer", params);
	}

	public User checkSysUser(String account, String password) {

		User user = new User();
		user.setAccount(account);
		user.setPassword(PasswordEncoder.encrypt(password, null));

		return this.selectOne(user);
	}

	public int save(User sysUser, Map params) {

		int count = 0;
		String user_uuid = sysUser.getEmployeeID();
		String passsword = sysUser.getPassword();
		// 新增
		if (PMSUtils.isEmpty(user_uuid)) {
			user_uuid = PMSUtils.getUUID();
			sysUser.setEmployeeID(user_uuid);;
			sysUser.setPassword(PasswordEncoder.encrypt(passsword, null));;
			count = this.insertSelective(sysUser);

		} else {
			if(PMSUtils.isEmpty(passsword)){
				sysUser.setPassword(null);// null站位
			}else{
				sysUser.setPassword(PasswordEncoder.encrypt(passsword, null));
			}
			this.updateByPrimaryKeySelective(sysUser);

		}

		return count;
	}
	
	public int delete(Map map) {
		int count = 0;

		String employeeId = PMSUtils.isNull(map.get("EMPLOYEEID"));
		count = this.deleteByPrimaryKey(employeeId);
		
		return count;
	}
	
	public int deleteUserRole(Map params){
		return dao.delete("userMapper.deleteBatchUserRole", params);
	}
	
	
	public void insertBatchUserRole(List userRoles){
		if(userRoles.size() > 0){
			dao.insert("userMapper.insertBatchUserRole", userRoles);
		}
	}
	
}
